package com.stu.thread;

public class Test2 {
  public static void main(String[] args) throws InterruptedException {
    AA aa = new AA();
    MT mt = new MT(aa);
    new Thread(mt).start();
    new Thread(mt).start();
    Thread.sleep(10000);
    System.out.println(aa.getA());
  }
}

class AA {
  private int a;
  public int getA() {
    return a;
  }
  public void addA() {
    for (int i = 0; i < 100000; i++) a++;
  }
}

class MT implements Runnable {
  private AA aa;
  public MT(AA aa) {
    this.aa = aa;
  }
  @Override
  public void run() {
    synchronized(this) {
      aa.addA();
    }
    // aa.addA();
  }
}

class AAA {
  private AA aa;
  public AAA(AA aa) { this.aa = aa; }

  public void add() {
    synchronized(aa) {
      aa.addA();
    }
    // aa.addA();
  }
}

class TT {
  public static void main(String[] args) throws InterruptedException {
    AA aa = new AA();
    AAA aaa = new AAA(aa);
    new Thread(() -> aaa.add()).start();
    new Thread(() -> aaa.add()).start();
    Thread.sleep(10000);
    System.out.println(aa.getA());
  }
}